<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\AliPayService;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Yansongda\Pay\Pay;

class PublicController extends Controller
{
    //

    public function orderList(Request $request){
        $orders = Order::orderBy('id', 'desc')->limit(500)->get();
        foreach ($orders as $order){
            switch ($order->trade_status) {
                case 'TRADE_SUCCESS':
                    $order->status = 'success';
                    break;
                default:
                    $order->status = 'waiting';
                    break;
            }

            $order->id = $order->out_trade_no;
            $order->email = $order->buyer_logon_id;
            $order->remark = $order->subject;
            $order->createTime = date('Y-m-d H:i:s', strtotime($order->created_at));
            $order->payTime = date('Y-m-d H:i:s',  strtotime($order->payment_time));
        }
        return activeSuccess($orders);

    }



    public function tradeQuery(Request $request){
        $order = Order::where('out_trade_no', $request->input('orderNo'))->first();
        if($order->trade_status == 'TRADE_SUCCESS'){
            return activeSuccess($order);
        }
        return activeFail();
    }

    public function order(Request $request){
        $out_trade_no = $request->input('id');
        $params = array(
            "out_trade_no" => $out_trade_no,
            "money"  => $request->input('amount'),
            "subject"  => $request->input('remark'),
            "notify_url"  => "https://pay.eiun.net/api/alipay/callback",
        );

        //创建支付宝对象
        $alipayobj= new AliPayService();
        $result = $alipayobj->facepay($params);

        Order::create([
            'out_trade_no' => $out_trade_no,
            'amount' => $request->input('amount'),
            'subject' => $request->input('remark'),
        ]);

        if ($result['status']){
            $qrcodePath = 'qrcodes/'.$out_trade_no.'.svg';
            \SimpleSoftwareIO\QrCode\Facades\QrCode::encoding('UTF-8')->size(200)->generate($result['data']['qr_code'],public_path('qrcodes/'.$out_trade_no.'.svg'));
            return activeSuccess($qrcodePath,$result['msg']);
        }else{
            return activeFail('',$result['msg']);
        }
    }




    /**
     * 服务器回调
     */
    public function alipayCallBack(Request $request){

        Log::info('支付宝回调参数：'.json_encode($request->all()));

        $params = $request->all();

        $orderInfo = Order::where('out_trade_no', $params['out_trade_no'])->first();

        $orderInfo->trade_status = $params['trade_status'];
        $orderInfo->payment_time = $params['gmt_payment'];
        $orderInfo->buyer_logon_id = $params['buyer_logon_id'];
        $orderInfo->save();
    }
    /**
     * 建立跳转请求表单
     * @param string $url 数据提交跳转到的URL
     * @param array $data 请求参数数组
     * @param string $method 提交方式：post或get 默认post
     * @return string 提交表单的HTML文本
     */
    function buildRequestForm($url, $data, $method = 'post', $button_name = '确定', $show = false) {
        $html = "<form id='requestForm' name='requestForm' action='" . $url . "' method='" . $method . "'>";
        while (list($key, $val) = each($data)) {
            $html.= "<input type='hidden' name='" . $key . "' value='" . $val . "' />";
        }
        $display = $show ? "style='display:block;'" : "style='display:none;'";
        $html.= "<input type='submit' value='" . $button_name . "' " . $display . "></form>";
        $html.= "<script>document.forms['requestForm'].submit();</script>";
        return $html;
    }










}
